import Vue from "vue";
import VueRouter from "vue-router";
import * as types from "../store/mutation-types";
import NProgress from "nprogress";

NProgress.configure({ showSpinner: false });

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    component: () =>
      import(/* webpackChunkName: "index" */ "../views/home/index.vue")
  },
  {
    path: "/pay",
    name: "pay",
    component: () =>
      import(/* webpackChunkName: "index" */ "../views/home/pay.vue")
  },
  {
    path: "/wxInner",
    name: "wx_inner",
    component: () => import("../views/home/wxInner.vue")
  },
  {
    path: "/scenic",
    name: "scenic",
    component: () => import("../views/home/scenicSpot.vue")
  }
];

const router = new VueRouter({
  // mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  if (to.meta.name) {
    document.title = to.meta.name;
  }
  next();
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const token = Vue.storage.get(types.ACCESS_TOKEN);
    if (token) {
      next();
    } else {
      next({
        path: "/login",
        query: {
          redirect: to.fullPath
        }
      });
    }
  } else {
    next();
  }
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
